﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace SBPweb.RIA.Client.Common.Collections
{

    public class ObservableList<TItem> : ObservableCollection<TItem>, IList<TItem>
    {

        #region Constructor logic

        public ObservableList()
            : base()
        {
        }

        public ObservableList(List<TItem> items)
#if !WINDOWS_PHONE
            : base(items)
#else
            : base()
#endif
        {
#if WINDOWS_PHONE
            foreach (var item in items)
            {
                Items.Add(item);
            }
#endif
        }

        #endregion

        #region Properties

        protected List<TItem> ItemsList
        {
            get
            {
                return Items as List<TItem>;
            }
        }

        #endregion

        #region Methods

        public void Sort()
        {
            if (ItemsList == null)
            {
                return;
            }
            ItemsList.Sort();
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void Sort(int index, int count, IComparer<TItem> comparer)
        {
            if (ItemsList == null)
            {
                return;
            }
            ItemsList.Sort(index, count, comparer);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void Sort(IComparer<TItem> comparer)
        {
            if (ItemsList == null)
            {
                return;
            }
            ItemsList.Sort(comparer);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        public void Sort(Comparison<TItem> comparison)
        {
            if (ItemsList == null)
            {
                return;
            }
            ItemsList.Sort(comparison);
            OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        #endregion

    }

}